package web_wms_mapper

import (
	"context"
	"gorm.io/gorm"
	"transport/web/web_vo"
	"transport/web/web_wms_model"
)

type OutboundOrderHeaderMapper struct {
	DB  *gorm.DB
	Ctx context.Context
}

func (mapper *OutboundOrderHeaderMapper) Create(m *web_wms_model.WmOutboundOrderHeader) (ret *web_wms_model.WmOutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderHeaderMapper) Update(m *web_wms_model.WmOutboundOrderHeader) (ret *web_wms_model.WmOutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderHeader{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderHeaderMapper) CreateOrUpdate(m *web_wms_model.WmOutboundOrderHeader) (ret *web_wms_model.WmOutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *OutboundOrderHeaderMapper) QueryById(id string) (m *web_wms_model.WmOutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderHeader{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *OutboundOrderHeaderMapper) QueryByStatusOk() (m []*web_vo.OutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Table("wm_outbound_order_header wooh").
		Select("wooh.*,rsp.pack_count ,wc.customer_code,wc.expression_cn,wc.route").
		Joins("LEFT JOIN ( SELECT wrts.order_code, SUM ( lcl_count ) AS pack_count FROM wm_review_task_summary wrts GROUP BY wrts.order_code ) AS rsp ON rsp.order_code = wooh.order_code").
		Joins("LEFT JOIN wm_customer wc ON wc.id = wooh.receiver_id").
		Where("(wooh.order_type = ? or wooh.order_type = ?) and wooh.status = ?", "CK", "CT", "1").Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *OutboundOrderHeaderMapper) QueryByIds(ids []string) (m []*web_wms_model.WmOutboundOrderHeader, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderHeader{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderHeaderMapper) DeleteById(id string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_wms_model.WmOutboundOrderHeader{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *OutboundOrderHeaderMapper) DeleteByIds(ids []string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_wms_model.WmOutboundOrderHeader{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *OutboundOrderHeaderMapper) Page(pageIndex, pageSize int) (items []*web_wms_model.WmOutboundOrderHeader, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *OutboundOrderHeaderMapper) List() (items []*web_wms_model.WmOutboundOrderHeader, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Find(&items).Group("id").Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
